﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace QD.UrlRewriter
{
    public class RewriteConfig : Config.ConfigsBase
    {
        private static RewriteConfig _instance = null;
        public static RewriteConfig Instance()
        {
            if (_instance == null)
                _instance = new RewriteConfig();

            return _instance;
        }

        protected override string ConfigFilePath
        {
            get { return HttpContext.Current.Server.MapPath("/Config/Urls.config"); }
        }

        protected override Type ConfigInfoType
        {
            get { return typeof(RewriteUrls); }
        }


        private RewriteUrls _rewriteUrls;

        public RewriteConfig()
        {
            Deserializa();
        }
        public RewriteUrls GetConfig()
        {
            return _rewriteUrls;
        }
        protected void Save()
        {
            SaveConfig(_rewriteUrls);

        }
        public void Save(mRewriteUrl model)
        {
            _rewriteUrls.Urls.Add(model);
            Save();
        }

        private void Deserializa()
        {

            try
            {
                _rewriteUrls = (RewriteUrls)LoadConfig();

            }
            catch (Exception)
            {
                _rewriteUrls = new RewriteUrls();
                _rewriteUrls.Urls = new List<mRewriteUrl>();
            }

        }

        public RewriteUrls GetRewriteUrls()
        {
            return _rewriteUrls;
        }
    }
}
